<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Bundle\Block\Adminhtml\Catalog\Product\Edit\Tab\Attributes\Extend */
$elementHtml = $block->getParentElementHtml();

$attributeCode = $block->getAttribute()
    ->getAttributeCode();

$switchAttributeCode = "{$attributeCode}_type";
$switchAttributeValue = $block->getProduct()
    ->getData($switchAttributeCode);

$isElementReadonly = $block->getElement()
    ->getReadonly();
?>

<?php if (!($attributeCode === 'price' && $block->getCanReadPrice() === false)) { ?>
    <div class="<?= /* @escapeNotVerified */  $attributeCode ?> "><?= /* @escapeNotVerified */  $elementHtml ?></div>
<?php } ?>

<?=$block->getExtendedElement($switchAttributeCode)->toHtml(); ?>

<?php if (!$isElementReadonly && $block->getDisableChild()) { ?>
    <script>
        require(['prototype'], function () {
            function <?= /* @escapeNotVerified */ $switchAttributeCode?>_change() {
                var $attribute = $('<?= /* @escapeNotVerified */ $attributeCode?>');
                if ($('<?= /* @escapeNotVerified */ $switchAttributeCode?>').value == '<?= /* @escapeNotVerified */ $block::DYNAMIC?>') {
                    if ($attribute) {
                        $attribute.disabled = true;
                        $attribute.value = '';
                        $attribute.removeClassName('required-entry');
                    }
                    if ($('dynamic-price-warning')) {
                        $('dynamic-price-warning').show();
                    }
                } else {
                    if ($attribute) {
                        <?php if ($attributeCode === 'price' && !$block->getCanEditPrice() && $block->getCanReadPrice()
                                && $block->getProduct()->isObjectNew()) { ?>
                            <?php $defaultProductPrice = $block->getDefaultProductPrice() ?: "''"; ?>
                            $attribute.value = <?= /* @escapeNotVerified */ $defaultProductPrice?>;
                        <?php } else { ?>
                            $attribute.disabled = false;
                            $attribute.addClassName('required-entry');
                        <?php } ?>
                    }
                    if ($('dynamic-price-warning')) {
                        $('dynamic-price-warning').hide();
                    }
                }
            }

            <?php if (!($attributeCode === 'price' && !$block->getCanEditPrice()
                    && !$block->getProduct()->isObjectNew())) { ?>
                $('<?= /* @escapeNotVerified */ $switchAttributeCode?>').observe('change', <?= /* @escapeNotVerified */ $switchAttributeCode?>_change);
            <?php } ?>
            Event.observe(window, 'load', function(){
                <?= /* @escapeNotVerified */ $switchAttributeCode?>_change();
            });
        });
    </script>
<?php } ?>
